'use strict';

const Service = require('egg').Service;

class UserService extends Service {
    /**
     * 根据用户名，查询用户
     * @param {*} param0
     */
    async findByEmail(email) {
        // 用户是否已注册
        const res = await this.app.mysql.query(
            'select * from user where email = ? limit 0, 1',
            [email]
        );
        return res[0];
    }

    /**
     * 添加用户
     * @param {*} email
     */
    async save({ id, email, password, nickname, cid }) {
        let sql = `
            insert into user 
                (id, email, password, nickname, cid, create_at)
            values
                (?, ?, ?, ?, ?, ?)
        `;
        return await this.app.mysql.query(sql, [
            id,
            email,
            password,
            nickname,
            cid,
            Date.now(),
        ]);
    }
}

module.exports = UserService;
